#!/bin/bash
# 本地测试 HoneyBadgerBFT (4个节点，batch=1000)

# 清理旧日志
rm -rf /home/HoneyBadgerBFT/logs
mkdir -p /home/HoneyBadgerBFT/logs

# 配置参数
N=4
F=1
BATCH=1000
BASE_PORT=8000

# 构建 peers 参数 (每个节点的peers不包含自己)
build_peers() {
    local my_id=$1
    local peers=""
    for i in $(seq 0 $((N-1))); do
        if [ $i -ne $my_id ]; then
            if [ -n "$peers" ]; then
                peers="${peers},"
            fi
            peers="${peers}${i}:localhost:$((BASE_PORT + i))"
        fi
    done
    echo "$peers"
}

echo "Starting HoneyBadgerBFT local test..."
echo "Parameters: N=$N, F=$F, BATCH=$BATCH"
echo ""

# 启动4个节点
for i in $(seq 0 $((N-1))); do
    PORT=$((BASE_PORT + i))
    PEERS=$(build_peers $i)
    LOG_FILE="/home/HoneyBadgerBFT/logs/node-${i}.out"
    
    echo "Starting Node $i on localhost:$PORT"
    echo "  Peers: $PEERS"
    
    nohup /home/HoneyBadgerBFT/honeybadger \
        -id $i \
        -addr "localhost:$PORT" \
        -peers "$PEERS" \
        -n $N \
        -f $F \
        -batch $BATCH \
        > "$LOG_FILE" 2>&1 &
    
    echo "  PID: $!"
    echo "  Log: $LOG_FILE"
    echo ""
done

echo "All nodes started!"
echo ""
echo "Monitor logs with:"
echo "  tail -f /home/HoneyBadgerBFT/logs/node-*.out"
echo ""
echo "Stop all nodes with:"
echo "  pkill -f honeybadger"

